/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.physics;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Joint;
import com.badlogic.gdx.physics.box2d.joints.GearJoint;
import com.badlogic.gdx.physics.box2d.joints.GearJointDef;
import com.jdiv.kernel.JDivSystem;

/** Gear joint. This definition requires two existing revolute or prismatic joints (any combination will work). The provided joints must attach a dynamic body to a static body. */

public class JointGear {
	
	GearJointDef jointDef;
	Joint joint;
	
	/**
	 * Constructor of JointGear.
	 * @param bodyA Body of object A.
	 * @param bodyB Body of object B.
	 * @param collideConnected Sets if they collide with each other.
	 */
	
 public JointGear( Body bodyA, Body bodyB, boolean collideConnected ){
		
	jointDef = new GearJointDef();
		
	joint = null;
	
	init( bodyA, bodyB, collideConnected );
 }
 
 /**
	 * Initialize the bodies.
	 * @param bodyA Body of object A.
	 * @param bodyB Body of object B.
	 * @param collideConnected Sets if they collide with each other.
	 */
	
 public void init ( Body bodyA, Body bodyB, boolean collideConnected ){
		
		if( joint != null ) return;
		
		jointDef.bodyA = bodyA;
		jointDef.bodyB = bodyB;
		jointDef.collideConnected = collideConnected;
 }
 
 /**
  * @param joint1 The first revolute/prismatic joint attached to the gear joint.
  */
 
 public void setJoint1( Joint joint1 ){
	 jointDef.joint1 = joint1;
 }
 
 /**
  * @param joint2 The second revolute/prismatic joint attached to the gear joint.
  */
 
 public void setJoint2( Joint joint2 ){
	 jointDef.joint2 = joint2;
 }
 
 /**
  * @param ratio The gear ratio.
  */
 
 public void setRatio( float ratio ){
	 jointDef.ratio = ratio;
 }
 
 /**
	 * Add the joint to the physical world.
	 * @return The joint in the physical world.
	 */
 
 public Joint addToWorld(){
	 if( joint != null ) return joint;
	 
	 if( JDivSystem.box2dWorld != null )	
	  joint = JDivSystem.box2dWorld.createJoint(jointDef);
	 
	 return joint;
 }
 
 /**
	 * @return Gets GearJoint of LibGdx.
	 */
 
 public GearJoint getGearJoint(){
	 return (GearJoint)joint;
 }
 
 /** Delete the joint in the physical world. */
 
 public void delete(){
		if( (JDivSystem.box2dWorld == null) || (joint == null) ) return;
		
		JDivSystem.box2dWorld.destroyJoint(joint);
		joint = null;
	}
 
}
